package com.live.shop.home.setting.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.live.shop.R;
import com.live.shop.aliyun.AliyunTtsUtil;
import com.live.shop.model.AliyunVoiceModel;
import com.live.shop.model.UserModel;
import com.live.shop.sql.UserDatabase;
import com.live.shop.tool.GetUtil;
import com.live.shop.view.ToastBottomUtils;

import java.util.ArrayList;
import java.util.List;

public class PageFragment extends Fragment implements OnItemClickListener{
    public RecyclerView fragment_page_recyclerview;
    VoiceAdapter adapter;
    List<AliyunVoiceModel> list = new ArrayList<>();
    private static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;
    public static PageFragment newInstance(int page) {
        PageFragment fragment = new PageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPage = getArguments().getInt(ARG_PAGE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_page, container, false);
        fragment_page_recyclerview = view.findViewById(R.id.fragment_page_recyclerview);
        loadData();
        return view;
    }
    @SuppressLint("NotifyDataSetChanged")
    private void loadData() {
        list.clear();
        // 加载数据到RecyclerView
        switch (mPage){
            case 0:list = GetUtil.getSceneVoiceTongyong();break;
            case 1:list = GetUtil.getSceneVoiceZhibo();break;
            case 2:list = GetUtil.getSceneVoiceKefu();break;
            case 3:list = GetUtil.getSceneVoiceDuoyuzhong();break;
            case 4:list = GetUtil.getSceneVoiceFangyan();break;
            case 5:list = GetUtil.getSceneVoiceYingwen();break;
            case 6:list = GetUtil.getSceneVoiceChildren();break;
        }
        adapter = new VoiceAdapter(getContext(), list);
        adapter.setOnItemClickListener(this);
        //瀑布流效果
        fragment_page_recyclerview.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        fragment_page_recyclerview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //取出当前用户配置
        UserDatabase userDb = new UserDatabase(getContext());
        UserModel userModel = userDb.select();
        String appkey = userModel.getAliyunAppkey();
        String AccessKeyID = userModel.getAliyunAccessKeyID();
        String AccessKeySecret = userModel.getAliyunAccessKeySecret();
        String speed_level = userModel.getAliyunSpeedLevel().isEmpty()?"1":userModel.getAliyunSpeedLevel();
        String volume = userModel.getAliyunVolume().isEmpty()?"1":userModel.getAliyunVolume();
        adapter.setAliyunFontame(userModel.getAliyunFontName());
        adapter.setAliyunConfigValue(appkey,AccessKeyID,AccessKeySecret,speed_level,volume);
    }
    @Override
    public void onItemClick(int position, String font_name) {
        mOnVoiceListItemSelect.onClick(position,font_name);
    }
    //定义变量
    private OnVoiceListItemSelect mOnVoiceListItemSelect;
    //定义触发方法
    public void setOnVoiceListItemSelect(OnVoiceListItemSelect onVoiceListItemSelect) {
        this.mOnVoiceListItemSelect = onVoiceListItemSelect;
    }
    //定义接口
    public interface OnVoiceListItemSelect {
        void onClick(int position, String font_name);
    }

    //设置参数值
    public void setAliyunConfig(String appkey,String AccessKeyID,String AccessKeySecret,String speed_level,String volume){
        adapter.setAliyunConfigValue(appkey,AccessKeyID,AccessKeySecret,speed_level,volume);
    }


    static class VoiceAdapter extends RecyclerView.Adapter{
        private OnItemClickListener listener;

        List<AliyunVoiceModel> list = new ArrayList<>();
        String appkey = "";
        String AccessKeyID = "";
        String AccessKeySecret = "";
        String font_name = "";
        String speed_level = "";
        String volume = "";
        Context mContext;
        public VoiceAdapter(Context context,List<AliyunVoiceModel> list) {
            this.mContext = context;
            this.list = list;
        }

        public void setAliyunConfigValue(String appkey,String AccessKeyID,String AccessKeySecret,String speed_level,String volume){
            this.appkey = appkey;
            this.AccessKeyID = AccessKeyID;
            this.AccessKeySecret = AccessKeySecret;
            this.speed_level = speed_level;
            this.volume = volume;
        }
        public void setAliyunFontame(String font_name){
            this.font_name = font_name;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new VoiceHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_aliyun_voice, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            VoiceHolder mholder = (VoiceAdapter.VoiceHolder) holder;
            if(list.get(position).getModelGender() == 1){
                mholder.item_aliyun_voice_img.setBackgroundResource(R.mipmap.icon_aliyun_male);
            }else{
                mholder.item_aliyun_voice_img.setBackgroundResource(R.mipmap.icon_aliyun_female);
            }
            mholder.item_aliyun_voice_name.setText(list.get(position).getModelName());
            mholder.item_aliyun_voice_type.setText(list.get(position).getModelType());
            mholder.item_aliyun_voice_audition.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //试听
                    if(appkey.isEmpty() || AccessKeyID.isEmpty() || AccessKeySecret.isEmpty()){
                        ToastBottomUtils.Setinfo(mContext,"请先配置密钥！");
                        return ;
                    }
                    //音量，Integer 范围是0~100，可选，默认50。
                    if(Float.valueOf(volume) < 0 ||  Float.valueOf(volume) > 100 ){
                        ToastBottomUtils.Setinfo(mContext,"音量设置范围不规范！");
                        return ;
                    }

                    //语速，Integer 取值范围：-500~500，默认值：0
                    if(Float.valueOf(speed_level) < -500 ||  Float.valueOf(speed_level) > 500 ){
                        ToastBottomUtils.Setinfo(mContext,"语速设置范围不规范！");
                        return ;
                    }
                    AliyunTtsUtil ttsUtil = new AliyunTtsUtil(appkey,AccessKeyID,AccessKeySecret);
                    //文本、音色、语速、音量
                    ttsUtil.postRequestPlayAudio(mContext,list.get(position).getModelAuditionstr(),list.get(position).getModelVoice(),speed_level,volume);
                    ToastBottomUtils.Setinfo(mContext,"合成中，请稍后..");
                }
            });
            mholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //获取到点击的声音名称，以便判断当前页是否含有
                    font_name = list.get(position).getModelVoice();
                    if (listener != null) {
                        listener.onItemClick(position,list.get(position).getModelVoice()); // 调用回调方法
                    }
                    notifyDataSetChanged();
                }
            });
            // 设置选中状态
            if (list.get(position).getModelVoice().equals(font_name)) {
                mholder.itemView.setBackgroundResource(R.drawable.bg_shape_dae6f2_10dip);
            } else {
                mholder.itemView.setBackgroundResource(R.drawable.bg_shape_white_10dip);
            }
            //通过tag将ViewHolder和itemView绑定
            mholder.itemView.setTag(this);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public int getItemCount() {
            return list.size();
        }

        static class VoiceHolder extends RecyclerView.ViewHolder {
            LinearLayout item_aliyun_voice_linearLayout;
            ImageView item_aliyun_voice_img;
            TextView item_aliyun_voice_name;
            TextView item_aliyun_voice_type;
            TextView item_aliyun_voice_audition;
            public VoiceHolder( View itemView) {
                super(itemView);
                item_aliyun_voice_linearLayout = itemView.findViewById(R.id.item_aliyun_voice_linearLayout);
                item_aliyun_voice_img = itemView.findViewById(R.id.item_aliyun_voice_img);
                item_aliyun_voice_name = itemView.findViewById(R.id.item_aliyun_voice_name);
                item_aliyun_voice_type = itemView.findViewById(R.id.item_aliyun_voice_type);
                item_aliyun_voice_audition = itemView.findViewById(R.id.item_aliyun_voice_audition);
            }
        }
        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            listener = onItemClickListener;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}